package io.geekidea.boot.otherstores.controller;
import cn.hutool.core.bean.BeanUtil;
import io.geekidea.boot.area.entity.Area;
import io.geekidea.boot.area.service.AreaService;
import io.geekidea.boot.auth.util.LoginUtil;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.otherstores.dto.addOtherStore;
import io.geekidea.boot.otherstores.dto.updateOtherStore;
import io.geekidea.boot.otherstores.entity.OtherStores;
import io.geekidea.boot.otherstores.entity.Stores;
import io.geekidea.boot.otherstores.query.OtherQuery;
import io.geekidea.boot.otherstores.service.OtherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/otherstores")
@Tag(name = "他店库存")
public class OtherStoresController {
    @Resource
    private OtherService otherService;
    @Resource
    private AreaService areaService;

    @PostMapping("/searchList")
    @Operation(summary = "查询",description = "按照多条件组合分页查询他店库存数据列表")
    public ApiResult searchList(@RequestBody OtherQuery otherQuery){

        List<OtherStores> byList = otherService.findByList(otherQuery);
        int total =otherService.getTotal(otherQuery).getTotal();
        PageDto<OtherStores> pageDto = new PageDto<>(byList,total,otherQuery.getPageSize(),otherQuery.getPageIndex()+1);

        return ApiResult.success(pageDto);
    }

    @PostMapping("/getStoreList/{areaId}")
    @Operation(summary = "查询门店列表")
    public ApiResult getAllStoreList(@PathVariable Integer areaId){

        List<Stores> storeList = otherService.getStoreList(areaId);
        return ApiResult.success(storeList);
    }

    @PostMapping("/getStore/{id}")
    @Operation(summary = "数据回填")
    public ApiResult  getStore(@PathVariable Integer id){

        OtherStores store = otherService.getStore(id);

        return ApiResult.success(store);
    }

    @PostMapping("/getAreaList")
    @Operation(summary = "查询战区列表")
    public ApiResult areaName(){
        List<Area> areas = areaService.list();
        return ApiResult.success(areas);
    }

    @PostMapping("/addOtherStore")
    @Operation(summary = "添加")
    public ApiResult addStore (@RequestBody addOtherStore addOtherStore) throws Exception {
        addOtherStore.setCreatedBy(LoginUtil.getUsername());
        boolean b = otherService.addStore(addOtherStore);
        if (!b) {
            return ApiResult.success("添加失败");
        }
        return ApiResult.success("添加成功");
    }

    @PostMapping("/updateOtherStore")
    @Operation(summary = "修改")
    public ApiResult updateOtherStore (@RequestBody updateOtherStore updateOtherStore) throws Exception {
        updateOtherStore.setUpdatedTime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        updateOtherStore.setUpdatedBy(LoginUtil.getUsername());
        boolean b = otherService.updateOtherStore(updateOtherStore);
        if (!b) {
            return ApiResult.success("修改失败");
        }
        return ApiResult.success("修改成功");
    }




    @PostMapping("/del/{id}")
    @Operation(summary = "逻辑删除",description = "根据id逻辑删除对应的列")
    public ApiResult del(@PathVariable Integer id){

        boolean update = otherService.delById(id);

        if (!update) {
            return ApiResult.success("删除失败");
        }
        return ApiResult.success("删除成功");

    }





}
